package com.cheer.ba.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.cheer.ba.R;
import com.cheer.ba.listener.OnItemClickListener;
import com.cheer.ba.model.MyAddressModel;
import com.cheer.ba.utils.TextViewUtils;
import com.cheer.lib_recycle_swipe.SwipeMenuAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 ：我的常用地址
 * 作者 ：hq
 * 时间 ：2017/7/2 16:13
 */
public class FrequentlyAreaAdp extends SwipeMenuAdapter<FrequentlyAreaAdp.DefaultViewHolder> {

    public static final int VIEW_TYPE_MENU = 1;
    public static final int VIEW_TYPE_NONE = 2;

    private List<MyAddressModel> titles;

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public FrequentlyAreaAdp(List<MyAddressModel> mAddres) {
        this.titles = mAddres;
    }

    @Override
    public int getItemViewType(int position) {
        MyAddressModel model = (MyAddressModel) getItem(position);
        int ismain = model.getIsMain();
        if (ismain == 1) {
            return VIEW_TYPE_NONE;
        } else {
            return VIEW_TYPE_MENU;
        }
    }

    @Override
    public int getItemCount() {
        return titles == null ? 0 : titles.size();
    }

    public Object getItem(int position) {
        if (null != titles && !titles.isEmpty()) {
            return titles.get(position);
        }
        return new MyAddressModel();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.adp_my_address, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        MyAddressModel model = (MyAddressModel) getItem(position);
        if (null != model) {
            holder.setName(model.getName());
            holder.setTel(model.getTel());
            if (model.getIsMain() == 1) {
                String msg = "[默认地址] " + model.getLocation().replaceAll("|", "");
                TextViewUtils.setSpannableStyleColor(msg, 0, 6, holder.mTxtAddress, R.color.red);
            } else {
                holder.setAddress(model.getProvince() + " " + model.getCity());
            }
            holder.setOnItemClickListener(mOnItemClickListener);
        }
    }


    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView mTxtName;

        TextView mTxtTel;

        TextView mTxtAddress;

        OnItemClickListener mOnItemClickListener;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mTxtName = (TextView) itemView.findViewById(R.id.id_name_txt);
            mTxtTel = (TextView) itemView.findViewById(R.id.id_phone_txt);
            mTxtAddress = (TextView) itemView.findViewById(R.id.id_detail_address_txt);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setName(String name) {
            this.mTxtName.setText(name);
        }

        public void setTel(String tel) {
            this.mTxtTel.setText(tel);
        }

        public void setAddress(String address) {
            this.mTxtAddress.setText(address);
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
